home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: [Help] I can't find my error.
- Date: 22 Feb 1996 07:53 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <22FEB199607532063@erich.triumf.ca>
- References: <4ggvgr$1b2@aurora.engr.LaTech.edu>
- NNTP-Posting-Host: erich.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4ggvgr$1b2@aurora.engr.LaTech.edu>, pluu@engr.LaTech.edu writes...
- >
- >Hi, all;
- >Could anyone please tell me why the following program won't work?
- >
- >
- >*********************************************************
- >/* This program will ask your name and age; and then
- >will print your name and age for next year back */
- >
- >#include <stdio.h>
- >
- >main()
- >{
- > char name;
-
- This reserves space to store _exactly_ one char - hardly enough for most names!
- Change it to
- char name[20]; /* or more - long enough to hold the largest */
- /* expected name */
-
- > int age, next_age;
- >
- > printf("%s\n","Please enter your name and age: ");
- > scanf("%s%d\n", &name, &age);
-
- This should be
- scanf("%19s%d\n", name, &age);
- to limit the number of chars to the number that will fit in name[]
-
- scanf() often causes problems when used for direct user input - it may be
- better to use fgets() into a suitable sized buffer, then sscanf() to parse the
- input.
-
-
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
-
-